ChatFrame1:AddMessage("\124cFF1987FFHex\124cFFFFFFFFThreat Loaded.")

-- HexThreat Main frame
local frame = CreateFrame("Frame", "HTMainFrame", UIParent)

-- Set default h/w
frame:SetHeight(15)
frame:SetWidth(200)

-- set default position
frame:SetPoint("CENTER", UIParent, "CENTER", 0, 0)

frame:EnableMouse(true)
frame:SetMovable(true)
frame:SetResizable(true)
frame:SetUserPlaced(true)
frame:SetClampedToScreen( true )

-- max sizes
frame:SetMaxResize(500,125);
frame:SetMinResize(125,12);

-- drag stuff
frame:RegisterForDrag("LeftButton");
frame:SetScript("OnDragStart", function(s) 
	if IsAltKeyDown() and IsShiftKeyDown() then s:StartMoving() 
	elseif IsShiftKeyDown() then s:StartSizing()
	end 
end)

frame:SetScript("OnDragStop", function(s) s:StopMovingOrSizing() end)
frame:SetScript("OnEnter", function(s) 
	if not UnitAffectingCombat("player") then
		GameTooltip:SetOwner(s, "ANCHOR_TOP")
		GameTooltip:AddLine("HexThreat Bar", 0, 1, 0.5, 1, 1, 1)
		GameTooltip:AddLine("Hold down SHIFT to resize.", 1, 1, 1, 1, 1, 1)
		GameTooltip:AddLine("Hold down ALT+SHIFT to move.", 1, 1, 1, 1, 1, 1)
		GameTooltip:AddLine("/htb hide to hide out of combat.", 1, 1, 1, 1, 1, 1)
		GameTooltip:Show()
	end
end)
frame:SetScript("OnLeave", function(s)
	GameTooltip:Hide()
end)

-- theme stuff
frame:SetBackdrop({
	bgFile = "Interface\\CastingBar\\UI-CastingBar-Border",
	edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
	tile = false,
	edgeSize = 16,
	insets = {
		left = -30,
		right = -30,
		top = -30,
		bottom = -30
	}
});
frame:SetBackdropColor(0, 0, 0, 0)
frame:SetBackdropBorderColor(0, 0, 0, 1)

frame:RegisterEvent("UNIT_THREAT_SITUATION_UPDATE")
frame:RegisterEvent("UNIT_THREAT_LIST_UPDATE")
frame:RegisterEvent("PLAYER_TARGET_CHANGED")
frame:RegisterEvent("PARTY_MEMBERS_CHANGED")

local threatbar = CreateFrame("StatusBar", "ThreatBar", frame)
threatbar:SetPoint("TOPLEFT", frame, "TOPLEFT", 4, -4)
threatbar:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", -4, 4)

--threatbar:SetHeight(25)
--threatbar:SetWidth(190)

threatbar:SetStatusBarTexture("Interface\\TARGETINGFRAME\\UI-StatusBar")
threatbar:SetMinMaxValues(0, 100)
threatbar:SetValue(0)
threatbar:SetBackdrop({ 
	bgFile = "Interface\\TARGETINGFRAME\\BarFill2",
	insets = {left = 1, right = 1, top = 1, bottom = 1}
})
threatbar:SetBackdropColor(0, 0, 0, .25)

local font1 = threatbar:CreateFontString("UnitName", "ARTWORK", "GameFontNormal")
font1:SetPoint("TOP", threatbar, "TOP", 0, 15)
local font2 = threatbar:CreateFontString("Percent", "OVERLAY", "GameFontNormal")
font2:SetPoint("CENTER", threatbar, "CENTER")
font2:SetTextColor(1, 1, 1)

SLASH_ThreatBar1 = "/htb";
local function handler(msg, editbox)
	if (msg=="show") then
		frame:Show()
		hidden = nil
	elseif (msg=="hide") then
		frame:Hide()
		hidden = true
		--[[elseif (msg=="lock") then
		frame:EnableMouse(nil)
		frame:SetMovable(nil)
		frame:SetResizable(nil)
		frame:SetScript("OnEnter", nil)
		frame:SetScript("OnLeave", nil)]]
	end
end
SlashCmdList["ThreatBar"] = handler;


local function GetThreat(unitId, mobId)
	local _, _, threatpct, _, _ = UnitDetailedThreatSituation(unitId, mobId)
	if not threatpct then threatpct = 0 end
	return floor(threatpct)
end

local function GetThreatStatus(unitId, mobId)
	local _,status,_,_, _ = UnitDetailedThreatSituation(unitId, mobId)
	return status
end

local function GetTank(unitId, mobId)
	local isTanking,_,_,_, _ = UnitDetailedThreatSituation(unitId, mobId)
	if not isTanking then isTanking = 0 end
	return isTanking
end

local function GetClassColor(unit)
	local unitClass, _ = UnitClass(unit)
	local classColor = "ff848484" --Pets
	if unitClass == "Death Knight" then
		classColor = "ffC41F3B"

	elseif unitClass == "Druid" then
		classColor = "ffFF7D0A"

	elseif unitClass == "Hunter" then
		classColor = "ffABD473"

	elseif unitClass == "Mage" then
		classColor = "ff69CCF0"

	elseif unitClass == "Paladin" then
		classColor = "ffF58CBA"

	elseif unitClass == "Priest" then
		classColor = "ffFFFFFF"

	elseif unitClass == "Rogue" then
		classColor = "ffFFF569"

	elseif unitClass == "Shaman" then
		classColor = "ff2459FF"

	elseif unitClass == "Warlock" then
		classColor = "ff9482C9"

	elseif unitClass == "Warrior" then
		classColor = "ffC79C6E"
	end
	return classColor
end

local function UpdateDisplay()
	font1:SetText("")
	font2:SetText("")
	threatbar:SetValue(0)
	threatbar:SetBackdropColor(0, 0, 0, .25)
	frame:SetBackdropBorderColor(0, 0, 0, 1)
	local status = nil
	local highUnit = ""
	local unitThreat = 0
	local highThreat = 0
	local tankName = nil
	local partynum = GetNumPartyMembers()
	local raidnum = GetNumRaidMembers()
	if hidden == true then -- hide frame
		frame:Hide()
	end
	if (partynum == 0 and raidnum == 0) and UnitCanAttack("player", "target") and not(UnitIsDead("target") or UnitIsFriend("player", "target") or UnitPlayerControlled("target")) -- Solo Target
		then			
			status = GetThreatStatus("player", "target")
			threatpct = GetThreat("player", "target")
			if HasPetUI() then
				highThreat = GetThreat("pet", "target")
				highUnit = UnitName("pet")
				highUnitColor = GetClassColor("pet")
				unitTanking = GetTank("pet", "target")
				if unitTanking == 1 then
					unitTankingColor = GetClassColor("pet")
					tankName = UnitName("pet")
				end
			end
		elseif (partynum == 0 and raidnum == 0) and UnitCanAttack("player", "targettarget") and not (UnitCanAttack("player", "target") or UnitIsDead("targettarget") or UnitIsFriend("player", "targettarget") or UnitPlayerControlled("targettarget")) -- Solo Target of Target
			then
				status = GetThreatStatus("player", "targettarget")
				threatpct = GetThreat("player", "targettarget")			
				if HasPetUI() then
					highThreat = GetThreat("pet", "targettarget")
					highUnit = UnitName("pet")
					highUnitColor = GetClassColor("pet")
					unitTanking = GetTank("pet", "targettarget")
					if unitTanking == 1 then
						unitTankingColor = GetClassColor("pet")
						tankName = UnitName("pet")
					end
				end
			elseif (raidnum > 0) and UnitCanAttack("player", "target") and not (UnitIsDead("target") or UnitIsFriend("player", "target") or UnitPlayerControlled("target")) --Raid Target
				then 
					status = GetThreatStatus("player", "target")
					threatpct = GetThreat("player", "target")
					for i = 1, (raidnum -1) do
						unitThreat = GetThreat("raid"..i, "target") 	
						if unitThreat > highThreat then 
							highThreat = unitThreat	
							highUnit = UnitName("raid"..i)
							highUnitColor = GetClassColor("raid"..i)
						end
						unitThreat = GetThreat("raidpet"..i, "target")	
						if unitThreat > highThreat then 
							highThreat = unitThreat	
							highUnit = UnitName("raidpet"..i)
							highUnitColor = GetClassColor("pet")
						end
						unitThreat = GetThreat("pet", "target")
						if unitThreat > highThreat then
							highThreat = unitThreat
							highUnit = UnitName("pet")
							highUnitColor = GetClassColor("pet")
						end
						unitTanking = GetTank("raid"..i, "target")
						if unitTanking == 1 then
							unitTankingColor = GetClassColor("raid"..i)
							tankName = UnitName("raid"..i)
						end
						unitTanking = GetTank("raidpet"..i, "target")
						if unitTanking == 1 then
							unitTankingColor = GetClassColor("raidpet"..i)
							tankName = UnitName("raidpet"..i)
						end
						unitTanking = GetTank("pet", "target")
						if unitTanking == 1 then
							unitTankingColor = GetClassColor("pet")
							tankName = UnitName("pet")
						end
					end
				elseif (raidnum > 0) and UnitCanAttack("player", "targettarget") and not (UnitCanAttack("player", "target") or UnitIsDead("targettarget") or UnitIsFriend("player", "targettarget") or UnitPlayerControlled("targettarget")) --Raid Target of Target
					then
						status = GetThreatStatus("player", "targettarget")
						threatpct = GetThreat("player", "targettarget")				
						for i = 1, (raidnum -1) do
							unitThreat = GetThreat("raid"..i, "targettarget") 	
							if unitThreat > highThreat then 
								highThreat = unitThreat	
								highUnit = UnitName("raid"..i)
								highUnitColor = GetClassColor("raid"..i)
							end
							unitThreat = GetThreat("raidpet"..i, "targettarget")	
							if unitThreat > highThreat then 
								highThreat = unitThreat	
								highUnit = UnitName("raidpet"..i)
								highUnitColor = GetClassColor("pet")
							end
							unitThreat = GetThreat("pet", "targettarget")
							if unitThreat > highThreat then
								highThreat = unitThreat
								highUnit = UnitName("pet")
								highUnitColor = GetClassColor("pet")
							end
							unitTanking = GetTank("raid"..i, "targettarget")
							if unitTanking == 1 then
								unitTankingColor = GetClassColor("raid"..i)
								tankName = UnitName("raid"..i)
							end
							unitTanking = GetTank("raidpet"..i, "targettarget")
							if unitTanking == 1 then
								unitTankingColor = GetClassColor("raidpet"..i)
								tankName = UnitName("raidpet"..i)
							end
							unitTanking = GetTank("pet", "targettarget")
							if unitTanking == 1 then
								unitTankingColor = GetClassColor("pet")
								tankName = UnitName("pet")
							end
						end
					elseif (partynum > 0) and UnitCanAttack("player", "target") and not (UnitIsDead("target") or UnitIsFriend("player", "target") or UnitPlayerControlled("target")) -- Party Target
						then 
							status = GetThreatStatus("player", "target")
							threatpct = GetThreat("player", "target")	
							for i = 1, partynum do
								unitThreat = GetThreat("party"..i, "target") 	
								if unitThreat > highThreat then 
									highThreat = unitThreat	
									highUnit = UnitName("party"..i)
									highUnitColor = GetClassColor("party"..i)
								end
								unitThreat = GetThreat("partypet"..i, "target")	
								if unitThreat > highThreat then 
									highThreat = unitThreat	
									highUnit = UnitName("partypet"..i)
									highUnitColor = GetClassColor("partypet"..i)
								end
								unitThreat = GetThreat("pet", "target")
								if unitThreat > highThreat then
									highThreat = unitThreat
									highUnit = UnitName("pet")
									highUnitColor = GetClassColor("pet")
								end
								unitTanking = GetTank("party"..i, "target")
								if unitTanking == 1 then
									unitTankingColor = GetClassColor("party"..i)
									tankName = UnitName("party"..i)
								end
								unitTanking = GetTank("partypet"..i, "target")
								if unitTanking == 1 then
									unitTankingColor = GetClassColor("partypet"..i)
									tankName = UnitName("partypet"..i)
								end
								unitTanking = GetTank("pet", "target")
								if unitTanking == 1 then
									unitTankingColor = GetClassColor("pet")
									tankName = UnitName("pet")
								end
							end
						elseif (partynum > 0) and UnitCanAttack("player", "targettarget") and not (UnitCanAttack("player", "target") or UnitIsDead("targettarget") or UnitIsFriend("player", "targettarget") or UnitPlayerControlled("targettarget")) --Party Target of Target
							then
								status = GetThreatStatus("player", "targettarget")
								threatpct = GetThreat("player", "targettarget")				
								for i = 1, partynum do
									unitThreat = GetThreat("party"..i, "targettarget") 	
									if unitThreat > highThreat then 
										highThreat = unitThreat	
										highUnit = UnitName("party"..i)
										highUnitColor = GetClassColor("party"..i)
									end
									unitThreat = GetThreat("partypet"..i, "targettarget")	
									if unitThreat > highThreat then 
										highThreat = unitThreat	
										highUnit = UnitName("partypet"..i)
										highUnitColor = GetClassColor("partypet"..i)
									end
									unitThreat = GetThreat("pet", "targettarget")
									if unitThreat > highThreat then
										highThreat = unitThreat
										highUnit = UnitName("pet")
										highUnitColor = GetClassColor("pet")
									end
									unitTanking = GetTank("party"..i, "targettarget")
									if unitTanking == 1 then
										unitTankingColor = GetClassColor("party"..i)
										tankName = UnitName("party"..i)
									end
									unitTanking = GetTank("partypet"..i, "targettarget")
									if unitTanking == 1 then
										unitTankingColor = GetClassColor("partypet"..i)
										tankName = UnitName("partypet"..i)
									end
									unitTanking = GetTank("pet", "target")
									if unitTanking == 1 then
										unitTankingColor = GetClassColor("pet")
										tankName = UnitName("pet")
									end
								end
							end
							if status then -- show frame
								if hidden == true and not frame:IsShown() then
									frame:Show()
								end
							end
							if status == 0
								then
									font2:SetText(string.format("%d %%", threatpct ))
									threatbar:SetValue(threatpct)
									threatbar:SetStatusBarColor(0, 1, 0, 1)
									frame:SetBackdropBorderColor(0, 1, 0, 1)
									if tankName
										then
											font1:SetText(string.format("|c%s%s|r", unitTankingColor, tankName ))
											font2:SetText(string.format("%d %%", threatpct ))
											threatbar:SetValue(threatpct)
										end
									elseif status == 1
										then
											font2:SetText(string.format("%d %%", threatpct ))
											threatbar:SetValue(threatpct)
											threatbar:SetStatusBarColor(1, 1, 0, 1)
											frame:SetBackdropBorderColor(1, 1, 0, 1)
											if tankName
												then
													font1:SetText(string.format("|c%s%s|r", unitTankingColor, tankName ))
													font2:SetText(string.format("%d %%", threatpct ))
													threatbar:SetValue(threatpct)
												end
											elseif status == 2
												then
													font2:SetText("WEAK AGGRO!")
													threatbar:SetValue(threatpct)
													threatbar:SetStatusBarColor(1, .5, 0, 1)
													frame:SetBackdropBorderColor(1, .5, 0, 1)
													if highThreat ~= 0
														then
															threatdiff = (100 - highThreat)
															font1:SetText(string.format("|c%s%s|r", highUnitColor, highUnit ))
															font2:SetText(string.format("WEAK AGGRO: %s %%", threatdiff ))
															threatbar:SetValue(threatdiff)
															threatbar:SetBackdropColor(1, .5, 0, .4)
														end	
													elseif status == 3
														then
															font2:SetText("YOU HAVE AGGRO!")
															threatbar:SetValue(threatpct)
															threatbar:SetStatusBarColor(1, 0, 0, 1)
															frame:SetBackdropBorderColor(1, 0, 0, 1)
															-- play sound here
															if unitTanking ~= 1
																then
																end

															if highThreat ~= 0
																then
																	threatdiff = (100 - highThreat)
																	font1:SetText(string.format("|c%s%s|r", highUnitColor, highUnit ))
																	font2:SetText(string.format("AGGRO: %s %%", threatdiff ))
																	threatbar:SetValue(threatdiff)
																	threatbar:SetBackdropColor(1, 0, 0, .4)
																end
															end
														end

														CreateBorder(HTMainFrame, 12, 1, 1, 1, 1)

														frame:SetScript("OnEvent", function() UpdateDisplay() end )